<?php
/**
 * Backup handling for resourcepage. Unlike some other backup files, 
 * the module's data structures are not documented here. Please see
 * the proper documentation (on wiki) for a database diagram and
 * detailed description.
 *
 * @copyright &copy; 2006 The Open University
 * @author s.marshall@open.ac.uk
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package package_name
 *//** */

require_once('module_resourcepage.php');
  
function resourcepage_backup_mods($bf,$preferences) {
    
    global $CFG;

    $status = true;

    //Iterate over resourcepage table
    $mods = get_recordset ('resourcepage','course',$preferences->backup_course,"id");
    if ($mods->RecordCount()) {
        while ($mod = rs_fetch_next_record($mods)) {
            if (backup_mod_selected($preferences,'resourcepage',$mod->id)) {
                $status &= resourcepage_backup_one_mod($bf,$preferences,$mod);
            }
        }
        rs_close($mods);    return $status;
    }
    
    return $status;
}

function resourcepage_backup_one_mod($bf,$preferences,$resourcepage) {

    global $CFG;

    if (is_numeric($resourcepage)) {
        $resourcepage = get_record('resourcepage','id',$resourcepage);
    }

    //Start mod
    $status=fwrite ($bf,start_tag("MOD",3,true));
    fwrite ($bf,full_tag("ID",4,false,$resourcepage->id));
    fwrite ($bf,full_tag("MODTYPE",4,false,"resourcepage"));
    fwrite ($bf,full_tag("NAME",4,false,$resourcepage->name));
    fwrite ($bf,full_tag("SHARED",4,false,$resourcepage->shared));
    fwrite ($bf,full_tag("PUBLICFEED",4,false,$resourcepage->publicfeed));
    fwrite ($bf,full_tag("REFSTYLE",4,false,$resourcepage->refstyle));
    fwrite ($bf,full_tag("EXPORTREFS",4,false,$resourcepage->exportrefs));
    fwrite ($bf,full_tag("AUTOCREATEINSTANCE",4,false,$resourcepage->autocreateinstance));
    fwrite ($bf,full_tag("AUTOCREATETYPE",4,false,$resourcepage->autocreatetype));
    $dbversion = get_field_select('modules', 'version', "name='resourcepage'");
    fwrite ($bf,full_tag("DBVERSION",4,false,$dbversion));
    
    // Defer to mod class for other backup
    try {
        module_resourcepage::$inst->backup(new xml_backup($bf,$preferences,4),$resourcepage,
            backup_userdata_selected($preferences,'resourcepage',$resourcepage->id),
            $preferences->backup_site_files);
    } catch(Exception $e) {
        print '<div><strong>Resourcepage: '.$resourcepage->name.'</strong></div>';
        if(debugging()) {
            print '<pre>';
            print $e->getMessage().' ('.$e->getCode().')'."\n";
            print $e->getFile().':'.$e->getLine()."\n";
            print $e->getTraceAsString();            
            print '</pre>';
        } else {
            print '<div><strong>Error</strong>: '.htmlspecialchars($e->getMessage()).' ('.$e->getCode().')</div>';
        }
        print '<div><strong>This backup has failed</strong>. Resolve this problem before continuing.</div>';
        $status=false; 
        exit;           // exiting because backup status code though returning failed does not stop backing up     
    }
          
    //End mod
    $status &= fwrite ($bf,end_tag("MOD",3,true));

    return $status;
}

////Return an array of info (name,value)
function resourcepage_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
    
    if (!empty($instances) && is_array($instances) && count($instances)) {
        $info = array();
        foreach ($instances as $id => $instance) {
            $info += resourcepage_check_backup_mods_instances($instance,$backup_unique_code);
        }
        return $info;
    }
    //First the course data
    $info[0][0] = get_string('modulenameplural','resourcepage');
    $info[0][1] = count_records('resourcepage','course',$course);
    
    //Now, if requested, the user_data
    if ($user_data) {
        module_resourcepage::$inst->backup_get_userdata_info($course,0,$info,1);
    }
    return $info;
}

////Return an array of info (name,value)
function resourcepage_check_backup_mods_instances($instance,$backup_unique_code) {
    //Basic data
    $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
    $info[$instance->id.'0'][1] = '';

    if (!empty($instance->userdata)) {
        module_resourcepage::$inst->backup_get_userdata_info(
            0,$instance->id,$info,$instance->id.'1');
    }
    
    return $info;
}


//Return a content encoded to support interactivities linking. Every module
//should have its own. They are called automatically from the backup procedure.
function resourcepage_encode_content_links ($content,$preferences) {

    global $CFG;

    $base = preg_quote($CFG->wwwroot,"/");

    //Link to the list of resourcepages
    $buscar="/(".$base."\/mod\/resourcepage\/index.php\?id\=)([0-9]+)/";
    $result= preg_replace($buscar,'$@RESOURCEPAGEINDEX*$2@$',$content);

    //Link to resourcepage view by moduleid
    $buscar="/(".$base."\/mod\/resourcepage\/view.php\?id\=)([0-9]+)/";
    $result= preg_replace($buscar,'$@RESOURCEPAGEVIEWBYID*$2@$',$result);

    return $result;
}

